#include <QCoreApplication>
#include <vector>
#include <string>
#include <iostream>
#include <iterator>
using namespace std;
class BinaryCode
{
public:
    BinaryCode();
    ~BinaryCode();
    vector <string> decode(string message);
private:
    string decode2(string msg,int init);
};
BinaryCode::BinaryCode()
{

}

vector<string> BinaryCode::decode(string message)
{
    vector<string> result;
    int invaidchar=0;
        for(string::iterator itStr =  message.begin();itStr!=message.end(); itStr++ )
          {
            if(*itStr!='0'&&*itStr!='1'&&*itStr!='2'&&*itStr!='3')
            {
                cout<<*itStr<<"Invaid char"<<"\n";
                invaidchar=1;
                break;
            }
           }
        if(message.size()>50)
            cout<<"Input overlength"<<"\n";
        else if(!invaidchar)
            {
            result.push_back(decode2(message,0));
            result.push_back(decode2(message,1));
            }
    return result;
}
string BinaryCode::decode2(string strCode,int init)
{
    string p="";
    vector<int> code,decode(50);
    int codeLength;
    int fail = 0;
    codeLength=(int)strCode.size();
    for (int i=0;i<codeLength;i++)
    {
        code.push_back(strCode[i]-'0');
    }
    switch(codeLength)
    {
    case 0:
        fail = 1;
        break;
    case 1:
        decode[0] = init;
        if (decode[0]!=code[0])
            fail = 1;
        break;
    case 2:
        decode[0]=init;
        if (code[0]-decode[0]<0)
            fail = 1;
        else
        decode[1]=code[0]-decode[0];
        if (code[1]!=decode[0]+decode[1])
           fail = 1;
        break;
    default:
        decode[0]=init;
        if (code[0]-decode[0]<0)
            fail = 1;
        else
        decode[1]=code[0]-decode[0];

        if (code[1]-code[0]<0)
            fail = 1;
        else
            decode[2]=code[1]-code[0];

        if (code[1]!=decode[0]+decode[1]+decode[2])
           fail = 1;

        for(int i = 3;i<codeLength; i++ )
        {
            if (code[i-1]-code[i-2]+decode[i-3]<0)
               {fail = 1;
                break;}
            else
            decode[i]=code[i-1]-code[i-2]+decode[i-3];
        }
        if  (code.back()!=decode[code.size()-2]+decode[code.size()-1])
            fail = 1;
    }
    if (fail)
        p="NONE";
    else {
            for(int i=0;i<codeLength;i++)
            p.push_back(decode[i]+'0');
    }
    return p;
}

BinaryCode::~BinaryCode()
{

}

int main(int argc, char *argv[])
{
    vector <string> result;
    QCoreApplication a(argc, argv);
    BinaryCode coder;
    result=coder.decode("11");
    cout<<result[0]<<","<<result[1];
    return a.exec();
}
